package com.wulinbus.controller;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fancy.common.security.MD5;
import com.fancy.common.utils.Config;
import com.fancy.common.utils.StringUtil;
import com.wulinbus.model.CreativeModel;
import com.wulinbus.model.ScriptModel;
import com.wulinbus.model.ScriptOwnModel;
import com.wulinbus.model.UserModel;
import com.wulinbus.service.CreativeService;
import com.wulinbus.service.ScriptService;
import com.wulinbus.service.UserService;
import com.wulinbus.utils.CasUtil;

@Controller
public class IndexController{

	@Resource
	private UserService userService;
	@Resource
	private CreativeService creativeService;
	@Resource
	private ScriptService scriptService;
	
	@RequestMapping(value = { "/", "index.html" })
	public String index(Model model,HttpServletRequest request) {
		List<CreativeModel> creativeList=creativeService.getMaxLikeCreativeList();
		List<Integer> uids = new ArrayList<Integer>();
		for (CreativeModel creativeModel : creativeList) {
			uids.add(creativeModel.getUid());
		}
		if (uids.size() > 0) {
			List<UserModel> userList = userService.getListByUid(uids);
			model.addAttribute("userList", userList);
		}
		List<ScriptModel> scriptList=scriptService.getMaxAttributeScriptList();
		List<Integer> uids_script=new ArrayList<Integer>();
		for(ScriptModel scriptModel:scriptList)
		{
			uids_script.add(scriptModel.getUid());
		}
		if (uids_script.size() > 0) {
			List<UserModel> userScriptList = userService.getListByUid(uids_script);
			model.addAttribute("userScriptList", userScriptList);
		}
		
		model.addAttribute("creativeList", creativeList);
		model.addAttribute("scriptList", scriptList);
		model.addAttribute("uid", CasUtil.getUid(request));
		return "index";
	}

	@RequestMapping("login.html")
	public String login(HttpServletRequest request) {
		String referer = request.getHeader("referer");
		if (referer == null || referer.contains("register.html") || referer.contains("user")) {
			referer = Config.getString("server.host");
		}
		String redirect=String.format("redirect:%scas/login?service=%s", Config.getString("casserver.host"), URLEncoder.encode(referer));
		System.err.println(redirect);
		return redirect;
	}

	@RequestMapping("logout.html")
	public String logout(HttpServletRequest request, HttpServletResponse response) {
		String referer = request.getHeader("referer");
		if (referer == null || referer.contains("register.html") || referer.contains("user")) {
			referer = Config.getString("server.host");
		}
		return String.format("redirect:%scas/logout?service=%s", Config.getString("casserver.host"), URLEncoder.encode(referer));
	}

	@RequestMapping("register.html")
	public String register() {
		return "comp_register";
	}

	/**
	 * 个人注册
	 */
	@RequestMapping(value = "per_register.json", method = { RequestMethod.POST, RequestMethod.GET })
	public void per_register(HttpServletResponse response, Model model, String email1, String pwd1, String comp1, String tele1) throws IOException {
		response.setContentType("application/json;charset=UTF-8");
		UserModel userModel = new UserModel();
		UserModel userModelE = userService.findUserByEmail(email1);
		UserModel userModelC = userService.findUserByComp(comp1);
		if (userModelE == null) {
			if (userModelC == null) {
				userModel.setEmail(email1);
				userModel.setPwd(MD5.md5(pwd1));
				userModel.setComp(comp1);
				userModel.setTele(tele1);
				Integer role = 2;
				userModel.setRole(role);
				userService.addCompUser(userModel);
				Integer current_uid = userModel.getUid();
//				Cookie cookie = new Cookie("uid", current_uid.toString());
//				cookie.setPath("/");
//				response.addCookie(cookie);
				response.getWriter().write("{\"success\":\"true\",\"uid\":" + current_uid + "}");
			} else {
				response.getWriter().write("{\"comp\":\"false\"}");
			}
		} else {
			response.getWriter().write("{\"email\":\"false\"}");
		}
	}

	/**
	 * 企业注册
	 * 
	 * @throws IOException
	 */
	@RequestMapping(value = "comp_register.json", method = { RequestMethod.POST, RequestMethod.GET })
	public void comp_register(HttpServletResponse response, Model model, HttpSession session, String email2, String pwd2, String comp2, String tele2)
			throws IOException {
		response.setContentType("application/json;charset=UTF-8");
		UserModel userModel = new UserModel();
		UserModel userModelE = userService.findUserByEmail(email2);
		UserModel userModelC = userService.findUserByComp(comp2);
		if (userModelE == null) {
			if (userModelC == null) {
				userModel.setEmail(email2);
				userModel.setPwd(MD5.md5(pwd2));
				userModel.setComp(comp2);
				userModel.setTele(tele2);
				Integer role = 1;
				userModel.setRole(role);
				userService.addCompUser(userModel);
				Integer current_uid = userModel.getUid();
//				Cookie cookie = new Cookie("uid", current_uid.toString());
//				cookie.setPath("/");
//				response.addCookie(cookie);
				response.getWriter().write("{\"success\":\"true\",\"uid\":" + current_uid + "}");
			} else {
				response.getWriter().write("{\"comp\":\"false\"}");
			}
		} else {
			response.getWriter().write("{\"email\":\"false\"}");
		}
	}

}